package io.renren.modules.onlinestudy.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.onlinestudy.entity.QuestionEntity;
import io.renren.modules.onlinestudy.service.QuestionService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import sun.dc.pr.PRException;


/**
 * @author zhubsh
 * @email sd_zhusbh@163.com
 * @date 2018-10-26 17:13:53
 */
@RestController
@RequestMapping("onlinestudy/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("onlinestudy:question:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<QuestionEntity> questionList = questionService.queryList(query);
        int total = questionService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(questionList, total, query.getLimit(), query.getCurrPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("onlinestudy:question:info")
    public R info(@PathVariable("id") String id) {
        QuestionEntity question = questionService.queryObject(id);

        return R.ok().put("question", question);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("onlinestudy:question:save")
    public R save(QuestionEntity question) {
        questionService.save(question);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("onlinestudy:question:update")
    public R update(QuestionEntity question) {
        questionService.update(question);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("onlinestudy:question:delete")
    public R delete(@RequestBody String[] ids) {
        questionService.deleteBatch(ids);

        return R.ok();
    }

}
